<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use App\Supervisor;
use App\Student;
use App\User;
use App\Reviewer;
use Excel;

class ThesisController extends Controller
{

    public function index($onlyUnselectedStudents = 1) {
       
    }

    public function statistic() {
        $user = \Auth::user();
        return view('admin.export_title', compact("user"));
    }

    public function download() {
        $user = \Auth::user();
        return view('admin.download', compact("user"));
    }

    public function export($statistic_type) {
        if ($statistic_type == 1) {
            self::export_title();
        } else if ($statistic_type == 2) {
            self::export_description();
        } else if ($statistic_type == 3) {
            self::export_proposal();
        } else if ($statistic_type == 4) {
            self::export_paper();
        } else if ($statistic_type == 5) {
            self::export_review();
        } else if ($statistic_type == 6) {
            self::export_math_supervise_comment();
        }
    }

    public function export_title() {
        $th = array("学生学号", "学生姓名", "学生班级", "学生专业", "学生手机号码", "导师工号", "导师姓名", "导师手机号码", "选题", "导师是否审批");
        $data = array();
        $data[] = $th;
        $students = Student::all();

        $th_no_title = array("学生学号", "学生姓名", "学生班级", "学生专业", "学生手机号码", "导师工号", "导师姓名", "导师手机号码");
        $no_title_data = array();
        $no_title_data[] = $th_no_title;


        $no_approve_data = array();
        $no_approve_data[] = $th;

        foreach ($students as $student) {
            $supervisor = $student->supervisors()->wherePivot('isApproved', true)->get()->first();
            $row = array($student->user->username, $student->user->name, $student->class, $student->major, $student->user->phone);
            $current = $student->title()->where('current', '>', 0)->first();
            $thesis = $student->thesis;
            if ($supervisor != null) {
                $row[] = $supervisor->user->username;
                $row[] = $supervisor->user->name;
                $row[] = $supervisor->user->phone;
                if ($thesis != null && $current != null) {
                    $row[] = $current->title;
                    $row[] = $thesis->approved ? "已审批" : "未审批";
                    if ($thesis->approved != 1) {
                        $no_approve_data[] = $row;
                    }
                } else {
                    $no_title_data[] = $row;
                }
            }
            $data[] = $row;
        }
        Excel::create('2019届毕业生选题结果', function($excel)  use($data, $no_title_data, $no_approve_data) {

            $excel->sheet('2019届毕业生选题结果', function($sheet)  use($data) {

                $sheet->setOrientation('landscape');
                $sheet->fromArray($data);
            });

             $excel->sheet('未选题学生名单', function($sheet)  use($no_title_data) {

                $sheet->setOrientation('landscape');
                $sheet->fromArray($no_title_data);
            });

             $excel->sheet('导师未审核名单', function($sheet)  use($no_approve_data) {

                $sheet->setOrientation('landscape');
                $sheet->fromArray($no_approve_data);
            });

        })->export('xls');
    }


    public function export_description() {
        $th = array("学生学号", "学生姓名", "学生班级", "学生专业", "学生手机号码", "导师工号", "导师姓名", "导师手机号码", "开题报告", "导师是否审批");
        $data = array();
        $data[] = $th;
        $students = Student::all();

        $th_no_description = array("学生学号", "学生姓名", "学生班级", "学生专业", "学生手机号码", "导师工号", "导师姓名", "导师手机号码");
        $no_description_data = array();
        $no_description_data[] = $th_no_description;


        $no_approve_data = array();
        $no_approve_data[] = $th;

        foreach ($students as $student) {
            $supervisor = $student->supervisors()->wherePivot('isApproved', true)->get()->first();
            $row = array($student->user->username, $student->user->name, $student->class, $student->major, $student->user->phone);
            $thesis = $student->thesis;
            $current = $student->description()->where('current', '>', 0)->first();
            $thesis = $student->thesis;
            if ($supervisor != null) {
                $row[] = $supervisor->user->username;
                $row[] = $supervisor->user->name;
                $row[] = $supervisor->user->phone;
                if ($thesis != null && $current != null) {
                    $row[] = $current->title;
                    $row[] = $thesis->description_approved ? "已审批" : "未审批";
                    if ($thesis->description_approved != 1) {
                        $no_approve_data[] = $row;
                    }
                } else {
                    $no_description_data[] = $row;
                }
            }
            $data[] = $row;
        }
        Excel::create('2019届毕业生开题报告结果', function($excel)  use($data, $no_description_data, $no_approve_data) {

            $excel->sheet('2019届毕业生开题报告结果', function($sheet)  use($data) {

                $sheet->setOrientation('landscape');
                $sheet->fromArray($data);
            });

             $excel->sheet('未提交开题报告学生名单', function($sheet)  use($no_description_data) {

                $sheet->setOrientation('landscape');
                $sheet->fromArray($no_description_data);
            });

             $excel->sheet('导师未审核开题报告名单', function($sheet)  use($no_approve_data) {

                $sheet->setOrientation('landscape');
                $sheet->fromArray($no_approve_data);
            });

        })->export('xls');
    }


    public function export_proposal() {
        $th = array("学生学号", "学生姓名", "学生班级", "学生专业", "学生手机号码", "导师工号", "导师姓名", "导师手机号码", "开题报告", "导师是否审批");
        $data = array();
        $data[] = $th;
        $students = Student::all();

        $th_no_description = array("学生学号", "学生姓名", "学生班级", "学生专业", "学生手机号码", "导师工号", "导师姓名", "导师手机号码");
        $no_description_data = array();
        $no_description_data[] = $th_no_description;


        $no_approve_data = array();
        $no_approve_data[] = $th;

        foreach ($students as $student) {
            $supervisor = $student->supervisors()->wherePivot('isApproved', true)->get()->first();
            $row = array($student->user->username, $student->user->name, $student->class, $student->major, $student->user->phone);
            $thesis = $student->thesis;
            $current = $student->proposal()->where('current', '>', 0)->first();
            $thesis = $student->thesis;
            if ($supervisor != null) {
                $row[] = $supervisor->user->username;
                $row[] = $supervisor->user->name;
                $row[] = $supervisor->user->phone;
                if ($thesis != null && $current != null) {
                    $row[] = $current->title;
                    $row[] = $thesis->proposal_approved ? "已审批" : "未审批";
                    if ($thesis->proposal_approved != 1) {
                        $no_approve_data[] = $row;
                    }
                } else {
                    $no_description_data[] = $row;
                }
            }
            $data[] = $row;
        }
        Excel::create('2019届毕业生任务书结果', function($excel)  use($data, $no_description_data, $no_approve_data) {

            $excel->sheet('2019届毕业生任务书结果', function($sheet)  use($data) {

                $sheet->setOrientation('landscape');
                $sheet->fromArray($data);
            });

             $excel->sheet('未提交任务书学生名单', function($sheet)  use($no_description_data) {

                $sheet->setOrientation('landscape');
                $sheet->fromArray($no_description_data);
            });

             $excel->sheet('导师未审核任务书名单', function($sheet)  use($no_approve_data) {

                $sheet->setOrientation('landscape');
                $sheet->fromArray($no_approve_data);
            });

        })->export('xls');
    }


    public function export_paper() {
        $th = array("学生学号", "学生姓名", "学生班级", "学生专业", "学生手机号码", "导师工号", "导师姓名", "导师手机号码", "开题报告", "导师是否审批");
        $data = array();
        $data[] = $th;
        $students = Student::all();

        $th_no_description = array("学生学号", "学生姓名", "学生班级", "学生专业", "学生手机号码", "导师工号", "导师姓名", "导师手机号码");
        $no_description_data = array();
        $no_description_data[] = $th_no_description;


        $no_approve_data = array();
        $no_approve_data[] = $th;

        foreach ($students as $student) {
            $supervisor = $student->supervisors()->wherePivot('isApproved', true)->get()->first();
            $row = array($student->user->username, $student->user->name, $student->class, $student->major, $student->user->phone);
            $thesis = $student->thesis;

            $current = $student->paper()->where('current', '>', 0)->first();
            $thesis = $student->thesis;
            if ($supervisor != null) {
                $row[] = $supervisor->user->username;
                $row[] = $supervisor->user->name;
                $row[] = $supervisor->user->phone;
                if ($thesis != null && $current != null) {
                    $row[] = $current->title;
                    $row[] = $thesis->paper_approved ? "已审批" : "未审批";
                    if ($thesis->paper_approved != 1) {
                        $no_approve_data[] = $row;
                    }
                } else {
                    $no_description_data[] = $row;
                }
            }
            $data[] = $row;
        }
        Excel::create('2019届毕业生论文结果', function($excel)  use($data, $no_description_data, $no_approve_data) {

            $excel->sheet('2019届毕业生论文结果', function($sheet)  use($data) {

                $sheet->setOrientation('landscape');
                $sheet->fromArray($data);
            });

             $excel->sheet('未提交论文学生名单', function($sheet)  use($no_description_data) {

                $sheet->setOrientation('landscape');
                $sheet->fromArray($no_description_data);
            });

             $excel->sheet('导师未审核论文名单', function($sheet)  use($no_approve_data) {

                $sheet->setOrientation('landscape');
                $sheet->fromArray($no_approve_data);
            });

        })->export('xls');
    }


    public function export_review() {
        $th = array("学生学号", "学生姓名", "学生班级", "学生专业", "学生手机号码", "评阅老师工号", "评阅老师姓名", "评阅老师手机号码", "评阅分数", "指导老师", "指导老师分数");
        $data = array();
        $data[] = $th;
        $reviewers = Reviewer::all();

        foreach ($reviewers as $reviewer) {
            $student = Student::find($reviewer->student_id);
            $reviewer = Supervisor::find($reviewer->supervisor_id);


            $grade = "学生未提交论文";
            if ($student->review_comment) {
                $grade = $student->review_comment->grade;
            } else if ($student->paper->count() > 0) {
                $grade = "导师未评阅";
            }
            $row = array($student->user->username, $student->user->name, $student->class, $student->major, 
                $student->user->phone, $reviewer->user->username, $reviewer->user->name, $reviewer->user->phone,
                $grade);
            $row[] = $student->supervisors()->wherePivot('isApproved', true)->get()->first()->user->name;
            if ($student->supervise_comment) {
                $row[] = $student->supervise_comment->grade;
            } else {
                $row[] = "指导老师未给分数";
            }
            $data[] = $row;
        }
        Excel::create('2019届毕业生评阅结果', function($excel)  use($data) {

            $excel->sheet('2019届毕业生评阅结果', function($sheet)  use($data) {

                $sheet->setOrientation('landscape');
                $sheet->fromArray($data);
            });


        })->export('xls');
    }

    public function export_math_supervise_comment() {
        $th = array("学生学号", "学生姓名", "学生班级", "学生专业", "学生手机号码", "指导老师", "指导老师分数", "评阅老师", "评阅老师分数", "答辩老师", "答辩老师分数");
        $data = array();
        $data[] = $th;
        $students = Student::all();

        foreach ($students as $student) {
            //$student = Student::find($reviewer->student_id);
            $row = array($student->user->username, $student->user->name, $student->class, $student->major, 
                $student->user->phone);
            $row[] = $student->supervisors()->wherePivot('isApproved', true)->get()->first()->user->name;
            if ($student->supervise_comment) {
                $row[] = $student->supervise_comment->grade;
            } else {
                $row[] = "指导老师未给分数";
            }

            //评阅老师
            $reviewer = $student->reviewer()->get()->first();
            if ($reviewer != null) {
                $row[] = $reviewer->user->name;
            } else {
                $row[] = "未选定评阅老师";
            }
            if ($student->review_comment) {
                $row[] = $student->review_comment->grade;
            } else {
                $row[] = "评阅老师未给分数";
            }

            // 答辩老师
            $defense_teacher = $student->defense_teacher()->get()->first();
            if ($defense_teacher != null) {
                $row[] = $defense_teacher->user->name;
            } else {
                $row[] = "未选定答辩老师";
            }
            if ($student->defense_comment) {
                $row[] = $student->defense_comment->grade;
            } else {
                $row[] = "答辩老师未给分数";
            }

            $data[] = $row;
        }
        Excel::create('2019届毕业生论文成绩汇总', function($excel)  use($data) {

            $excel->sheet('2019届毕业生论文成绩汇总', function($sheet)  use($data) {

                $sheet->setOrientation('landscape');
                $sheet->fromArray($data);
            });


        })->export('xls');
    }

}
